<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <th:block th:include="include :: header('机构管理列表')"/>
    <style>
        .layui-card-body .layui-form {
            margin: auto;
        }
        .layui-form-item {
            margin-bottom: 0px;
        }

    </style>
</head>
<body>
<div class="layuimini-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form id="formId" class="layui-form layui-form-pane">
            <div class="layui-form-item">
                <label class="layui-form-label">机构简称</label>
                <div class="layui-input-inline">
                    <input type="text" name="nameAbbr" placeholder="请输入机构简称"
                           class="layui-input" />
                </div>
                <label class="layui-form-label">机构编码</label>
                <div class="layui-input-inline">
                    <input type="text" name="code" placeholder="请输入机构编码"
                           class="layui-input"  lay-required/>
                </div>
                <button class="layui-btn layui-btn-md" lay-submit lay-filter="dept-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="layui-btn layui-btn-md layui-btn-primary" id="formreset">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
        <table id="dept-table" lay-filter="dept-table"></table>
        <script type="text/html" id="dept-toolbar">

            <button class="layui-btn" id="btn-expand">展开</button>
            <button sec:authorize="hasPermission('/sys/dept/add','sys:dept:add')"
                    class="layui-btn layui-btn-primary layui-btn-md" lay-event="add">
                <i class="layui-icon layui-icon-add-1"></i>
                新增
            </button>
            <button sec:authorize="hasPermission('/sys/dept/remove','sys:dept:del')"
                    class="layui-btn layui-btn-danger layui-btn-md" lay-event="batchRemove">
                <i class="layui-icon layui-icon-delete"></i>
                删除
            </button>
        </script>
        <script type="text/html" id="dept-bar">
            <button sec:authorize="hasPermission('/sys/dept/edit','sys:dept:edit')"
                    class="layui-btn layui-btn-primary layui-btn-sm" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>
            </button>
            <button sec:authorize="hasPermission('/sys/dept/remove','sys:dept:del')"
                    class="layui-btn layui-btn-danger layui-btn-sm" lay-event="remove"><i
                    class="layui-icon layui-icon-delete"></i>
            </button>
        </script>
    </div>
</div>
</div>

<script id="dept-status" type="text/html">
    <input type="checkbox" name="enable" value="{{d.deptId}}" lay-skin="switch" lay-text="启用|禁用"
           lay-filter="dept-enable" {{ d.status == '1' ? 'checked' : '' }}>
</script>

<th:block th:include="include :: footer"/>
<script>
    let table;
    layui.use(['table', 'form', 'jquery', 'treetable', 'dictionary'], function () {
        table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let treetable = layui.treetable;

        let prefix = pName + "/system/dept/";

        let cols =  [
            [
                {type: 'checkbox'},
                {field: 'nameAbbr', minWidth: 200, title: '机构简称'},
                {field: 'code', title: '机构编码'},
                {field: 'status', title: '状态', templet: '#dept-status'},
                {field: 'sort', title: '排序', width: 100},
                {title: '操作', templet: '#dept-bar', width: 120, align: 'center'}
            ]
        ]

        table.on('tool(dept-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(dept-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            }
        });

        form.on('submit(dept-query)', function (data) {
            window.query(data);
            return false;
        });

        window.query = function (data) {
            let loading = layer.load();
            $.ajax({
                url: prefix + 'list',
                data: data.field,
                dataType: 'json',
                contentType: 'application/json',
                type: 'get',
                success: function (result) {
                    layer.close(loading);
                    if (result.code == 0) {
                        treetable.render({
                            treeColIndex: 1,
                            treeSpid: result.data[0].parentId,
                            treeIdName: 'id',
                            treePidName: 'parentId',
                            skin: 'line',
                            method: 'get',
                            treeDefaultClose: true,
                            toolbar: '#dept-toolbar',
                            elem: '#dept-table',
                            data: result.data,
                            page: false,
                            cols: cols
                        });
                        $('#btn-expand').click(function () {
                            let text = $('#btn-expand').text();
                            if(text == '折叠'){
                                treetable.foldAll('#dept-table');
                                $('#btn-expand').text("展开");
                            }else {
                                treetable.expandAll('#dept-table');
                                $('#btn-expand').text("折叠");
                            }

                        });
                    } else {
                        layer.alert(result.msg, {icon: 2});
                    }
                }
            })
        }
        window.add = function () {
            let layerIndex = layer.open({
                type: 2,
                title: '新增机构管理',
                shade: 0.1,
                maxmin : true,
                area: ['550px', '500px'],
                content: prefix + 'add'
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.edit = function (obj) {
            let layerIndex = layer.open({
                type: 2,
                title: '修改机构管理',
                shade: 0.1,
                maxmin : true,
                area: ['550px', '500px'],
                content: prefix + 'edit?id=' + obj.data['id']
            });
            $(window).on("resize", function () {
                layer.full(layerIndex);
            });
            return false;
        }

        window.remove = function (obj) {
            layer.confirm('确定要删除该机构管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 1000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些机构管理', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: prefix + "batchRemove",
                    dataType: 'json',
                    data: {"ids": ids},
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 1) {
                            layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                                table.reload('dept-table');
                            });
                        } else {
                            layer.alert(result.msg, {icon: 2});
                        }
                    }
                })
            });
        }

        window.refresh = function (param) {
            table.reload('dept-table', {where: param});
        }

        $('#formreset').click(function(){
            $('#formId').find('input').val("")
        })
        window.query({});

    })
</script>
</body>
</html>